<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Role as RM;
use app\admin\model\Auth;
use app\admin\validate\RoleValidate;


class Role extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    
    /**
    * 角色列表
    */
    public function index()
    {
        $model = new RM();
        $data = $model->_list();
        $count = $model->_count();
        
        
        $this->assign([
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        
        return $this->fetch();
    }

    /**
    * 角色添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new RoleValidate();
            if(!$validate->scene(RoleValidate::SCENE_ADD)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $mode = new RM();
            $ret = $mode->_create($request);
            if($ret){
                die(outputSuccess([], '添加成功'));
            }else{
                die(outputError($mode->getError()));
            }
        }else{
            $auth_model = new Auth();
            $data = $auth_model->_getAuthAll();
            $list = make_tree($data);
            //dump($list);die;
            $this->assign([
                'list'=>$list,
            ]);
            return $this->fetch();
        }
        
    }
    
    
    /**
    * 角色编辑
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new RoleValidate();
            if(!$validate->scene(RoleValidate::SCENE_MODIFY)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $mode = new RM();
            $ret = $mode->_modify($request);
            if($ret){
                die(outputSuccess([], '编辑成功'));
            }else{
                die(outputError($mode->getError()));
            }
        }else{
            $id = (int)$this->request->get('id');
            $auth_model = new Auth();
            $data = $auth_model->_getAuthAll();
            $list = make_tree($data);
            //dump($list);die;
            $model = new RM();
            $data = $model->_findOne($id);
            if(!empty($data['auth_ids'])) {
                $data['auth_ids'] = explode(',', $data['auth_ids']);
            }else{
                $data['auth_ids'] = [];
            }
            $this->assign([
                'list' => $list,
                'data' => $data,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除角色
    */
    public function del(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new RoleValidate();
            if(!$validate->scene(RoleValidate::SCENE_REMOVE)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $mode = new RM();
            $id = (int)$request['id'];
            $ret = $mode->_remove($id);
            if($ret){
                die(outputSuccess([], '删除成功'));
            }else{
                die(outputError($mode->getError()));
            }
        }else{
            die(outputError('请求错误'));
        }
    }
}
